<template>
  <div class="app-container">
    <el-form :model="formInline" ref="form" label-width="90px" class="form-inline" label-suffix="：">
      <el-row :gutter="10">
        <el-col :lg="6" :xl="4"><el-form-item label="保单号">
            <el-input v-model="formInline.insureNum" placeholder="请输入保单号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="订单号">
            <el-input v-model="formInline.orderNum" placeholder="请输入订单号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="箱号">
            <el-input v-model="formInline.containerNumber" placeholder="请输入箱号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="运单号">
            <el-input v-model="formInline.tagInvoiceWaybill" placeholder="请输入运单号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="开票状态">
            <el-select v-model="formInline.opnenInvoiceStatus" placeholder="请选择批单状态">
              <el-option label="全部" value=""></el-option>
              <el-option label="未开票" value="0"></el-option>
              <el-option label="已开票" value="1"></el-option>

            </el-select>
          </el-form-item></el-col>
        <el-col :lg="1" :xl="1">
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table ref="table" :data="tableData">
      <el-table-column align="center" prop="orderNum" label="订单号" width="220">
      </el-table-column>

      <el-table-column align="center" prop="insuredName" label="被保险人" width="180">
      </el-table-column>

      <el-table-column align="center" prop="opnenInvoiceStatus" label="开票状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.opnenInvoiceStatus === '0'">未开票</el-tag>
          <el-tag v-if="scope.row.opnenInvoiceStatus === '1'" type="success">已开票</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="reportStatus" label="保险出险状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportStatus === '0'">未立案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '1'" type="warning">报案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '2'" type="warning">立案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '3'" type="danger">取消报案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '4'" type="warning">勘察/定损</el-tag>
          <el-tag v-if="scope.row.reportStatus === '5'" type="warning">核赔</el-tag>
          <el-tag v-if="scope.row.reportStatus === '6'" type="success">付款</el-tag>
          <el-tag v-if="scope.row.reportStatus === '7'" type="success">结案</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="reportStatus" label="订单状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderStatus === '2'">支付成功</el-tag>
          <el-tag v-if="scope.row.orderStatus === '3'" type="success">投保成功</el-tag>
          <el-tag v-if="scope.row.orderStatus === '4'" type="danger">投保失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="insureFee" label="保险金额(元)"> </el-table-column>
      <el-table-column align="center" prop="goodsName" label="货物名称"> </el-table-column>
      <el-table-column align="center" width="270" prop="payTime" label="投保时间"> </el-table-column>
      <el-table-column align="center" width="270" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="view(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-upload" @click="returnPolicy(scope.row)">上传发票</el-button>

          <el-button v-if="scope.row.opnenInvoiceStatus === '1'" size="mini" type="text" icon="el-icon-download"><a :href="scope.row.insureUrl" download="发票">下载</a></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 20px" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.pageNum" :page-sizes="[10, 20, 50]"
      :page-size="page.pageSize" layout="->, total, sizes, prev, pager, next, jumper" :total="page.total">
    </el-pagination>
    <view-or-examine @initTable="initTable" ref="viewOrExamine"></view-or-examine>
    <returnPolicyDialog @initTable="initTable" ref="returnPolicyDialog"></returnPolicyDialog>
  </div>
</template>
<script>
import viewOrExamine from "../guaranteeSlip/viewOrExamine.vue";
import returnPolicyDialog from "./returnPolicyDialog.vue";

export default {
  components: { viewOrExamine, returnPolicyDialog },
  data() {
    return {
      showCancelOrder: false,
      orderRow: {},
      cancelOrderForm: {
        orderStatusRemark: "",
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      formInline: {
        insureNum: "",
        orderNum: "",
        containerNumber: "",
        tagInvoiceWaybill: "",
        opnenInvoiceStatus: "",
      },
      tableData: [],
      tableHeight: 0,
    };
  },
  methods: {
    onSubmit() {
      this.page.pageNum = 1;
      this.getTableData();
    },
    returnPolicy(row) {
      this.$refs.returnPolicyDialog.dialogFormVisible = true;
      this.$refs.returnPolicyDialog.row = row;
      this.$refs.returnPolicyDialog.fileList = row.invoiceUrl
        ? [
            {
              name: row.invoiceUrl.substring(
                row.invoiceUrl.lastIndexOf("/") + 1
              ),
              url: row.invoiceUrl,
            },
          ]
        : [];
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getTableData();
    },
    view(row) {
      this.$refs.viewOrExamine.dialogFormVisible = true;
      this.$refs.viewOrExamine.title = "查看";
      this.$refs.viewOrExamine.isExamine = false;
      this.$refs.viewOrExamine.row = row;
      this.$refs.viewOrExamine.insureUrl = row.insureUrl;
      this.$refs.viewOrExamine.form.insureNum = row.insureNum;
    },
    setTableHeight() {
      this.$nextTick(() => {
        const queryConditionHeight = this.$refs.form.$el.offsetHeight;
        this.tableHeight =
          window.innerHeight - queryConditionHeight - 180 + "px";
      });
    },
    initTable() {
      this.$refs.viewOrExamine.dialogFormVisible = false;
      this.page.pageNum = 1;
      this.getTableData();
    },
    getTableData() {
      const params = {
        ...this.formInline,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      };
      console.log(params, params);
      this.$get("/warranty/invoice/list", params).then((res) => {
        this.tableData = res.rows;
        this.page.total = res.total;
      });
    },
  },
  mounted() {
    window.onresize = () => {
      this.setTableHeight();
    };
    this.setTableHeight();
    this.getTableData();
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>